//
//  MyInfoViewController.m
//  Kfangchan
//
//  Created by 朋飞 on 14-11-12.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "MyInfoViewController.h"
#import "UploadViewController.h"
#import "EditComViewController.h"

#pragma mark 个人资料
#pragma mark
@interface MyInfoViewController ()

@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet UILabel *phoneLabel;

@property (strong, nonatomic) IBOutlet UILabel *regtimeLabel;
@property (strong, nonatomic) IBOutlet UIView *shadowView1;
@property (strong, nonatomic) IBOutlet UIView *shadowView2;




@property (strong, nonatomic) IBOutlet UILabel *comnameLabel;


@property (strong, nonatomic) IBOutlet UILabel *identitynumLabel;
@property (strong, nonatomic) IBOutlet UIImageView *idcardImage;

@property (strong, nonatomic) IBOutlet UIButton *addidentityBtn;


@property (strong, nonatomic) IBOutlet UILabel *banknumLabel;


@property (strong, nonatomic) IBOutlet UIView *editpsdView;

@property (strong, nonatomic) IBOutlet UIButton *exitBtn;

@property (strong, nonatomic) IBOutlet UIButton *rzButton;

@property (strong, nonatomic) IBOutlet UIView *registerView;
@property (strong, nonatomic) IBOutlet UIView *compView;
@property (strong, nonatomic) IBOutlet UIView *idcardView;
@property (strong, nonatomic) IBOutlet UIView *bankNoView;

@end

@implementation MyInfoViewController

#pragma mark didload
#pragma mark
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.titleView = [[Theam currentTheam] navigationTitleViewWithTitle:@"个人资料"];
    self.navigationItem.leftBarButtonItem = [[Theam currentTheam] navigationBarLeftButtonItemWithImage:[UIImage imageNamed:@"nav_back.png"] Target:self Selector:@selector(back)];
    
    self.exitBtn.origin = CGPointMake(self.exitBtn.origin.x, self.editpsdView.origin.y + self.editpsdView.height + 20);
    
    NSDictionary *agent = [AppConfig getAgent];
    if (agent.count == 0) {
        [self goLogin:YES];
    }
    self.shadowView1.layer.shadowColor = [UIColor blackColor].CGColor;
    self.shadowView1.layer.shadowOffset = CGSizeMake(0, 1);
    self.shadowView1.layer.shadowOpacity = 0.1;
    self.shadowView1.layer.shadowRadius = 1;
    
    self.shadowView2.layer.shadowColor = [UIColor blackColor].CGColor;
    self.shadowView2.layer.shadowOffset = CGSizeMake(0, 1);
    self.shadowView2.layer.shadowOpacity = 0.1;
    self.shadowView2.layer.shadowRadius = 1;
    
    
    self.regtimeLabel.text =agent[AGENT_addTime];
    self.nameLabel.text = agent[AGENT_name];
    CGSize size = [self.nameLabel.text sizeWithFont:self.nameLabel.font constrainedToSize:CGSizeMake(self.nameLabel.width, self.nameLabel.height)];
    self.nameLabel.size = size;
    self.phoneLabel.text = agent[AGENT_mobile];
    
    UIImageView *registerLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"partition.png"]];
    registerLine.frame = CGRectMake(0, 44, 320, 0.5);
    [self.registerView addSubview:registerLine];
    
    UIImageView *compLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"partition.png"]];
    compLine.frame = CGRectMake(0, 44, 320, 0.5);
    [self.compView addSubview:compLine];
    
    UIImageView *idcardLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"partition.png"]];
    idcardLine.frame = CGRectMake(0, 44, 320, 0.5);
    [self.idcardView addSubview:idcardLine];
    
    UIImageView *bankNoLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"partition.png"]];
    bankNoLine.frame = CGRectMake(0, 44, 320, 0.5);
    [self.bankNoView addSubview:bankNoLine];
}

-(void)viewWillAppear:(BOOL)animated {
    NSDictionary *agent = [AppConfig getAgent];
    // 审核通过不需要再请求
    if (1 != [agent[AGENT_status] intValue]) {
        [self getUserInfo];
    } else {
        self.rzButton.userInteractionEnabled = NO;
        self.rzButton.size = CGSizeMake(52, 19);
        
        [self.rzButton setImage:[UIImage imageNamed:@"personal_rzjjr.png"] forState:UIControlStateNormal];
        self.rzButton.center = CGPointMake(self.nameLabel.center.x + self.nameLabel.width / 2 + self.rzButton.width / 2 + 10, self.nameLabel.center.y);
    }
    self.comnameLabel.text = agent[AGENT_company];
    
    NSString* idcard =  replaceIdCardNum(agent[AGENT_idcard]);
    self.identitynumLabel.text = idcard;
    if(idcard == nil || idcard.length == 0){
        self.idcardImage.hidden = NO;
    }else{
        self.idcardImage.hidden = YES;
    }
    self.banknumLabel.text = replaceBankCardNum(agent[AGENT_bankNo]);
    
}

#pragma mark 返回
#pragma mark
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 添加公司
#pragma mark{
- (IBAction) addCom:(id)sender{
    NSString *comName =[AppConfig getAgent][AGENT_company];
   if(comName!=nil&&comName.length>0){
        EditComViewController *editcom_vc = [[EditComViewController alloc] init];
        editcom_vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:editcom_vc animated:YES];
//                AddComViewController *addcom_vc = [[AddComViewController alloc] init];
//                addcom_vc.hidesBottomBarWhenPushed = YES;
//                [self.navigationController pushViewController:addcom_vc animated:YES];
    
    }else{
        AddComViewController *addcom_vc = [[AddComViewController alloc] init];
        addcom_vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:addcom_vc animated:YES];
        
    }
}

#pragma mark 添加身份证
#pragma mark{
- (IBAction) addIdentityNum:(id)sender{
    NSString* idcard = [AppConfig getAgent][AGENT_idcard];
    if(idcard ==nil || idcard.length ==0){
        
        AddIdentityViewController *addIdentity_vc = [[AddIdentityViewController alloc] init];
        addIdentity_vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:addIdentity_vc animated:YES];
        
    }
}

#pragma mark 添加银行卡号
#pragma mark{
- (IBAction) addBankNum:(id)sender{
    AddBankViewController *addBank_vc = [[AddBankViewController alloc] init];
    addBank_vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:addBank_vc animated:YES];
    
}

#pragma mark 修改密码
#pragma mark{
- (IBAction) editpsd:(id)sender{
    EditPsdViewController *editpsd_vc = [[EditPsdViewController alloc] init];
    editpsd_vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:editpsd_vc animated:YES];
    
}

#pragma mark 退出登录
#pragma mark{
- (IBAction) exit:(id)sender{
    [AppConfig removeAgent];
    [self goLogin:YES];
    [self.navigationController popViewControllerAnimated:NO];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark -
#pragma mark 上传页
- (IBAction)goUpload {
    UploadViewController *uvc = [[UploadViewController alloc] init];
    [self.navigationController pushViewController:uvc animated:YES];
}

- (void)getUserInfo {
    __weak MyInfoViewController *weak_self = self;
    MKNetworkOperation *operation = [[BaseEngine sharedEngine]RunRequest:nil path:KFC_UPDATE completionHandler:^(id responseObject) {
        if(responseObject){
            id obj = [responseObject objectForKey:@"agent"];
            Agent *agent = [[Agent alloc] initWithDictionary:obj];
            agent.KFC_AGENT_MOBILE = [responseObject objectForKey:KFC_AGENT_MOBILE];
            agent.KFC_AGENT_PWD = [responseObject objectForKey:KFC_AGENT_PWD];
            agent.KFC_AGENT_UCID = [responseObject objectForKey:KFC_AGENT_UCID];
            [AppConfig setAgent:[agent dictionary]];
            
            int status = agent.status;
            // 审核通过
            if (status == 1) {
                weak_self.rzButton.userInteractionEnabled = NO;
                weak_self.rzButton.size = CGSizeMake(52, 19);
                
                [weak_self.rzButton setImage:[UIImage imageNamed:@"personal_rzjjr.png"] forState:UIControlStateNormal];
            }
            // 注册未认证|审核不通过
            else if (status == 0 || status == 4) {
                weak_self.rzButton.size = CGSizeMake(63, 22);
                [weak_self.rzButton setImage:[UIImage imageNamed:@"personal_ljrz.png"] forState:UIControlStateNormal];
            }
            // 审核中
            else if (status == 3) {
                weak_self.rzButton.userInteractionEnabled = NO;
                weak_self.rzButton.size = CGSizeMake(63, 22);
                [weak_self.rzButton setImage:[UIImage imageNamed:@"personal_shz.png"] forState:UIControlStateNormal];
            }
            weak_self.rzButton.center = CGPointMake(weak_self.nameLabel.center.x + weak_self.nameLabel.width / 2 + weak_self.rzButton.width / 2 + 10, weak_self.nameLabel.center.y);
        }
    } errorHandler:^(NSError *error){
        showCustomAlertMessage(@"网路错误");
    }finishHandler:^(id responseObject) {
    }];
    
    [self.opHandlers addObject:operation];
}

@end
